<template>
    <div class="layout" id="building">
        <div class="container" v-if="isReday === 2">
            <div class="header">
                <!-- 个人信息 -->
                <div class="left" v-if="writer !== null">
                    <img class="userImg" :src="writer.img" alt="" />
                    <div class="userName">{{ writer.userName }}</div>
                </div>
                <!-- 返回按钮 -->
                <div class="right" @click="back()">
                    <img class="backImg" src="/button.svg" alt="">
                </div>
            </div>
            <div class="gridcontainer">
                <!-- 图片 -->
                <div class="swiper">
                    <img :src="article.cover" alt="">
                </div>
                <!-- 内容和评论 -->
                <div class="main">
                    <div class="message">
                        <div class="title">{{ article.title }}</div>
                        <div class="desc">
                            {{ article.content }}
                        </div>
                        <span style="color: aliceblue;font-size: calc(100vw * 16 / 1920);">评论:</span>
                        <div v-if="commentList.length>0">
                            <comment class="comment" v-for="item in commentList" :user="props.userList.get(item.uid)" :key="item.commentId" :comment="item"></comment>
                        </div>
                        <div v-else style="color: aliceblue;">评论区空空如也</div>
                    </div>
                    <div class="input">
                        <img :src="props.user.img" alt="">
                        <input v-model="content" @keyup.enter="uploadContent">
                        <button @click="uploadContent">提交</button>
                    </div>
                </div>

            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import comment from "../components/comment.vue";
import axios from "axios";
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from "element-plus";
const props = defineProps(['user','userList'])
const route = useRoute()
const router = useRouter()

const articleId = route.query.articleId;
const uid = route.query.uid

const isReday = ref(0)
const content = ref('')

const article = ref(null)
const writer = ref(null)

const commentList = ref([])
const back = function () {
    router.back()
}

onMounted(() => {

    // 发起请求获取文章
    axios.get(`/api/articles/${articleId}`)
        .then(res => {
            article.value = res.data.data
            if (article.value.cover === null) {
                article.value.cover = '/public/zzzLogo.png'
            }
            isReday.value++
        }).catch(e => {
			ElMessage({
				message:"获取服务器数据异常，请联系管理员",
				type:"error",
			})
	})

    axios({
        url:"/api/comments",
        method:"GET",
        params:{
            articleId:articleId
        }
    }).then(res=>{
        writer.value = props.userList.get(Number(uid))
        commentList.value = res.data.data;
        isReday.value++
    }).catch(e => {
			ElMessage({
				message:"获取服务器数据异常，请联系管理员",
				type:"error"
			})
	})
})

const uploadContent = function() {
    if(props.user.uid === 0) {
        ElMessage({
            message:"登录后才可以发表评论",
            offset:100,
            type:'info'
        })
        return 
    }else if(content.value === '') {
        console.log(content.value)
        ElMessage({
            message:"发表内容不能为空",
            offset:100,
            type:'error'
        })
        return
    }
    axios.post('/api/comments',{
        articleId:articleId,
        uid:props.user.uid,
        content:content.value
    },{headers:{'token':localStorage.getItem('token')}})
    .then(res=>{
        if(res.data.code === 1) {
            ElMessage({
                message:"发表评论成功",
                offset:100,
                type:'success'
            })
            let temp = {
                articleId:articleId,
                uid:props.user.uid,
                content:content.value,
                commentId:commentList.value.length === 0?1:commentList.value[commentList.value.length-1].commentId + 1
            }
            commentList.value.unshift(temp)
            content.value = ''
        }else {
            ElMessage({
                message:"发表失败，服务器出现异常",
                offset:100,
                type:'error'
            })
        }
    }).catch(e => {
			ElMessage({
				message:"获取服务器数据异常，请联系管理员",
				type:"error"
			})
	})
}

</script>

<style lang="scss" scoped>
.header {
    width: 60%;
    height: 9%;
    background-color: #ccc;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    background-color: rgb(37, 34, 34);
    border-radius: 20px 20px 0 0;

    .left {
        position: relative;
        display: flex;
        align-items: center;
        flex-direction: row;
    }

    .right {
        .backImg {
            margin-right: calc(100vw * 35 / 1920);
            width: calc(100vw * 65 / 1920);
            height: calc(100vw * 65 / 1920);
            cursor: pointer;
        }
    }

    .userImg {
        width: calc(100vw * 60 / 1920);
        height: calc(100vw * 60 / 1920);
        border-radius: calc(100vw * 60 / 1920);
    }

    .userName {
        margin-left: calc(100vw * 20 / 1920);
        font-size: calc(100vw * 24 / 1920); 
        color: rgb(161, 161, 161);
    }
}

.gridcontainer {
    height: 60%;
    width: 60%;
    background-color: #111;
    display: flex;

    .swiper {
        box-sizing: border-box;
        width: 45%;
        height: 100%;
        background-color: #111;
        display: flex;
        justify-content: center;
        align-items: center;

        img {

            display: block;
            width: 90%;
            height: 90%;
            border: calc(100vw * 6 / 1920) solid rgba(100, 100, 100, .5);
            border-radius: calc(100vw * 30 / 1920);
            object-fit: contain;
        }
    }

    .main {
        width: 55%;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: calc(100vw * 40 / 1920);
    }
}


.layout {
    width: 100%;
    height: 100%;
    margin-top: calc(100vh * 60 /1920);
}

.container {
    width: 99%;
    height: 99%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border: 2px solid rgb(50, 50, 50);
    border-radius: 60px;
    overflow: hidden;
}

.message {
    height: 80%;
    width: 90%;
    border: calc(100vw * 5 / 1920) solid #333;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: scroll;

    .title {
        box-sizing: border-box;
        margin-bottom: 10px;
        padding: 2px;
        height: 5% ;
        width: 100%;
        color: #fff;
        word-break: break-all;
        font-size:calc(100vw * 24 / 1920);
    }

    .desc {
        box-sizing: border-box;
        margin-bottom: 5px;
        line-height: 1.2;
        text-indent: 2em;
        padding: 2px;
        color: #b2b0b3;
        font-size:calc(100vw * 16 / 1920);
    }
}

.message::-webkit-scrollbar {
    display: none;
}

.input {
    width: 100%;
    height: 10%;
    position: relative;
    display: flex;
    background-color: #000;

    img {
        // position: absolute;
        // top: -3px;
        // right: -3px;
        // bottom: -3px;
        // left: -3px;
        border:calc(100vw *3 / 1920) solid #302e31;
        border-radius: calc(100vw * 60 / 1920);
        content: "";
        width: calc(100vw * 60 / 1920);
        height: calc(100vw * 60 / 1920);
    }

    input {
        font-family: 'zzz-font';
        width: calc(100vw * 420 / 1920);
        font-size: calc(100vw * 16 / 1920);;
    }

    button {
        font-family: 'zzz-font';
        font-size: calc(100vh * 14 / 1920);
        width: calc(100vw * 60 / 1920);
    }
}

</style>